﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Shared;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinDataSource;

namespace RLZY
{
    public partial class CustomColumnChooser : Form
    {
        public CustomColumnChooser()
        {
            InitializeComponent();
        }
        private UltraGridBase grid = null;
        /// <summary>
        /// Gets or sets the UltraGrid instances whose columns are displayed in the column chooser.
        /// </summary>
        public UltraGridBase Grid
        {
            get
            {
                return this.grid;
            }
            set
            {
                if (value != this.grid)
                {
                    this.grid = value;

                    this.ultraGridColumnChooser1.SourceGrid = this.grid;
                    this.InitializeBandsCombo(this.grid);

                    // Select the first band in the band selector.
                    if (this.ultraComboBandSelector.Rows.Count > 0)
                        this.ultraComboBandSelector.SelectedRow = this.ultraComboBandSelector.Rows[0];
                }
            }
        }
        #region InitializeBandsCombo

        private void InitializeBandsCombo(UltraGridBase grid)
        {
            this.ultraComboBandSelector.SetDataBinding(null, null);
            if (null == grid)
                return;

            // Create the data source that we can bind to UltraCombo for displaying 
            // list of bands. The datasource will have two columns. One that contains
            // the instances of UltraGridBand and the other that contains the text
            // representation of the bands.
            UltraDataSource bandsUDS = new UltraDataSource();
            bandsUDS.Band.Columns.Add("Band", typeof(UltraGridBand));
            bandsUDS.Band.Columns.Add("DisplayText", typeof(string));

            foreach (UltraGridBand band in grid.DisplayLayout.Bands)
            {
                if (!this.IsBandExcluded(band))
                {
                    bandsUDS.Rows.Add(new object[] { band, band.Header.Caption });
                }
            }

            this.ultraComboBandSelector.DisplayMember = "DisplayText";
            this.ultraComboBandSelector.ValueMember = "Band";
            this.ultraComboBandSelector.SetDataBinding(bandsUDS, null);

            // Hide the Band column.
            this.ultraComboBandSelector.DisplayLayout.Bands[0].Columns["Band"].Hidden = true;

            // Hide the column headers.
            this.ultraComboBandSelector.DisplayLayout.Bands[0].ColHeadersVisible = false;

            // Set some properties to improve the look & feel of the ultra combo.
            this.ultraComboBandSelector.DropDownWidth = 0;
            this.ultraComboBandSelector.DisplayLayout.Override.HotTrackRowAppearance.BackColor = Color.LightYellow;
            this.ultraComboBandSelector.DisplayLayout.AutoFitStyle = AutoFitStyle.ResizeAllColumns;
            this.ultraComboBandSelector.DisplayLayout.BorderStyle = UIElementBorderStyle.Solid;
            this.ultraComboBandSelector.DisplayLayout.Appearance.BorderColor = SystemColors.Highlight;
        }

        #endregion // InitializeBandsCombo
        #region IsBandExcluded

        /// <summary>
        /// Checks to see if the ExcludeFromColumnChooser property of the band or
        /// any of its ancestor bands is set to True.
        /// </summary>
        /// <param name="band"></param>
        /// <returns></returns>
        private bool IsBandExcluded(UltraGridBand band)
        {
            while (null != band)
            {
                if (ExcludeFromColumnChooser.True == band.ExcludeFromColumnChooser)
                    return true;

                band = band.ParentBand;
            }

            return false;
        }

        #endregion // IsBandExcluded

    }
}
